package com.eascs.ticket.ticket.service;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.common.assertion.util.AssertWarnUtils;
import com.eascs.ticket.common.entity.StatusConstant;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.ticket.entity.TicketReleaseNote;
import com.eascs.ticket.ticket.entity.TicketReleaseNoteDelete;
import com.eascs.ticket.ticket.entity.TicketReleaseNoteUpdate;
import com.eascs.ticket.ticket.entity.TicketReleaseNoteView;

@Service
public class TicketReleaseNoteService extends BaseSevice {
	@Inject
	private TicketChangeLogService ticketChangeLogService;

	public TicketReleaseNote getTicketReleaseNoteById(String id) {
		return this.proxyDao.getById(TicketReleaseNote.class, id);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void deleteTicketReleaseNoteById(TicketReleaseNoteDelete ticketReleaseNoteDelete) {
		TicketReleaseNote ticketReleaseNote = this.getTicketReleaseNoteById(ticketReleaseNoteDelete.getId());
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketReleaseNote.getTicketId(), "删除发布注意事项", ticketReleaseNoteDelete);
		this.proxyDao.updateById(ticketReleaseNoteDelete);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void updateTicketReleaseNoteById(TicketReleaseNoteUpdate ticketReleaseNoteUpdate) {
		TicketReleaseNote ticketReleaseNote = this.getTicketReleaseNoteById(ticketReleaseNoteUpdate.getId());
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketReleaseNote.getTicketId(), "修改发布注意事项", ticketReleaseNoteUpdate);
		this.proxyDao.updateById(ticketReleaseNoteUpdate);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketReleaseNoteById(TicketReleaseNote ticketReleaseNote) {
		AssertWarnUtils.assertNull(getValidTicketReleaseNoteByTicketId(ticketReleaseNote.getTicketId()), "该工单已经存在发布注意事项");
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketReleaseNote.getTicketId(), "增加发布注意事项", ticketReleaseNote);
		this.proxyDao.insert(ticketReleaseNote);
	}

	public TicketReleaseNote getValidTicketReleaseNoteByTicketId(String ticketId) {
		TicketReleaseNote ticketReleaseNote = new TicketReleaseNote();
		ticketReleaseNote.setTicketId(ticketId);
		ticketReleaseNote.setStatus(StatusConstant.STATUS_OK);
		return this.proxyDao.findEntityWithAny(ticketReleaseNote);
	}

	public TicketReleaseNoteView getValidTicketReleaseNoteViewByTicketId(String ticketId) {
		return this.proxyDao.findEntity("sql.TicketReleaseNoteService.getValidTicketReleaseNoteViewByTicketId", TicketReleaseNoteView.class, ticketId, StatusConstant.STATUS_OK);
	}
}
